const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const ZipPlugin = require("zip-webpack-plugin");
const CrxPlugin = require("crx-webpack-plugin");
// Generate pages object
const pagesObj = {};

const chromeName = ["popup", "options"];

chromeName.forEach(name => {
  pagesObj[name] = {
    entry: `src/${name}/index.js`,
    template: "public/index.html",
    filename: `${name}.html`
  };
});

const mainfest =
  process.env.NODE_ENV === "production"
    ? [
      {
        from: path.resolve("src/manifest.production.json"),
        to: `${path.resolve("dist")}/manifest.json`
      }
    ]
    : [
      {
        from: path.resolve("src/manifest.development.json"),
        to: `${path.resolve("dist")}/manifest.json`
      }
    ];

const plugins = [new CopyWebpackPlugin(mainfest)];


// 开发环境将热加载文件复制到dist文件夹
if (process.env.NODE_ENV !== 'production') {
  plugins.push(
    new CopyWebpackPlugin([{
      from: path.resolve("src/utils/hot-reload.js"),
      to: path.resolve("dist")
    }])
  )
}

// 添加图标
plugins.push(
  new CopyWebpackPlugin([{
    from: path.resolve("src/icon/"),
    to: path.resolve("dist/img")
  }])
);

if (process.env.NODE_ENV === 'production') {

  // plugins.push(
  //   new CrxPlugin({
  //     keyFile:  path.resolve("dist")+"/key.pem",
  //     contentPath: path.resolve("dist"),
  //     outputPath: path.resolve("dist"),
  //     name: "git-md-toc"
  //   })
  // );
  
  plugins.push(
    new ZipPlugin({
      path: path.resolve("dist"),
      filename: 'git-md-toc.zip',
    })
  );
}

module.exports = {
  pages: pagesObj,
  configureWebpack: {
    plugins: plugins,
    entry: {
      'content': './src/content/index.js'
    },
    output: {
      filename: 'js/[name].js'
    }
  },
  css: {
    extract: {
      filename: 'css/[name].css'
      // chunkFilename: 'css/[name].css'
    },
  },
  chainWebpack: config => {
    const fontsRule = config.module.rule('fonts');
    fontsRule.uses.clear()
    fontsRule.test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i)
      .use('url')
      .loader('url-loader')
      .options({
        limit: 1000,
        name: 'fonts/[name].[ext]'
      });
    if (process.env.npm_config_report) {
      // 在运行命令中添加 --report参数运行， 如：npm run build --report
      config
        .plugin('webpack-bundle-analyzer')
        .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
    }
  }

};
